namespace ChumChase.Services
{
    using System;
    using System.Windows.Threading;

    public class Scheduler : IScheduler
    {
        public Scheduler()
        {
            RefreshInterval = 180;
        }

        public int CountDown { get; set; }

        public int RefreshInterval { get; set; }

        #region IScheduler Members

        public void Start()
        {
            var timer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 0, 1), IsEnabled = true};
            timer.Tick += (s, e) => Tick();
            CountDown = RefreshInterval;
        }

        #endregion

        public event EventHandler OnTick;

        public void Tick()
        {
            if (OnTick != null) OnTick(this, EventArgs.Empty);
            CountDown--;
            if (CountDown >= 0) return;

            CountDown = RefreshInterval;
        }
    }
}